package tree.递归;

import po.TreeNode;

/**
 * @author songZiHao
 * @version 1.0.0
 * @ClassName 另一个树的子树572.java
 * @Description https://leetcode-cn.com/problems/subtree-of-another-tree/
 * @createTime 2021年06月16日 19:26:00
 */
public class 另一个树的子树572 {

    public boolean isSubtree(TreeNode root, TreeNode subRoot) {
        if (root == null) {
            return false;
        }
        if (subRoot == null) {
            return true;
        }
        return compare(root, subRoot) || isSubtree(root.left, subRoot) || isSubtree(root.right, subRoot);
    }

    private boolean compare(TreeNode root, TreeNode subRoot) {
        if (root == null && subRoot == null) {
            return true;
        }
        if (root == null || subRoot == null) {
            return false;
        }
        return root.val == subRoot.val && compare(root.left, subRoot.left) && compare(root.right, subRoot.right);
    }

}